<?php
/**
 * Skyline :: Patterns & component framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Skyline\Structure\Set;

use ArrayIterator;
use InvalidArgumentException;
use SplObjectStorage;

/**
 * Set backed by an SplObjectStorage instance that only stores objects
 * 
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class ObjectSet extends AbstractSet
{
	/**
	 * @var SplObjectStorage
	 */
	protected $elements;
	
	/**
	 * @var mixed[]
	 */
	protected $elementsArray;
	
	/**
	 * Class constructor
	 * 
	 * @param Traversable $elements [optional]
	 * @return void
	 */
	public function __construct($elements = null)
	{
		parent::__construct($elements);
		
		$this->elements = new SplObjectStorage();
	}
	
	/**
	 * Add an element to the collection
	 * 
	 * @param mixed $element
	 * @return void
	 */
	public function add($element)
	{
		if(!is_object($element)) {
			throw new InvalidArgumentException('Only object elements are supported by ObjectSet');
		}
		$this->assertNotFull();
		$this->assertElementIsValid($element);
		if($this->elements->contains($element)) {
			return false;
		}
		$this->elements->attach($element);
		$this->elementsArray = null;
		return true;
	}
	
	/**
	 * Clears this container of all its elements
	 * @return void
	 */
	public function clear()
	{
		$this->elements = new SplObjectStorage();
		$this->elementsArray = null;
	}
	
	/**
	 * Returns true if an element exists in the collection
	 * 
	 * @param mixed $element
	 * @return bool
	 */
	public function contains($element)
	{
		return $this->elements->contains($element);
	}
	
	/**
	 * Removes an element from the collection
	 * 
	 * @param mixed $element
	 * @return void
	 */
	public function remove($element)
	{
		$this->elements->detach($element);
		$this->elementsArray = null;
	}
	
	/**
	 * Returns this set as a simple PHP array
	 * @return mixed[]
	 */
	public function toArray()
	{
		if(is_null($this->elementsArray)) {
			$this->elementsArray = iterator_to_array($this->elements, false);
			if(!is_null($this->comparator)) {
				usort($this->elementsArray, array($this->comparator, 'compare'));
			}
		}
		return $this->elementsArray;
	}
	
	/**
	 * Returns the number of elements in this set
	 * @return int
	 */
	public function count()
	{
		return $this->elements->count();
	}
	
	/**
	 * Returns an iterator for the elements in this set
	 * @return Iterator
	 */
	public function getIterator()
	{
		if(is_null($this->comparator)) {
			return $this->elements;
		}
		return new ArrayIterator($this->toArray());
	}
}